package cn.tedu.pojo;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class TestMybatis {
    @Test
    public void get() throws IOException {
        //读取mybatis核心配置文件
        InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
        //构建会话工厂
        SqlSessionFactory session = new SqlSessionFactoryBuilder().build(in);
        //开启会话,准备执行sql
        SqlSession sqlSession = session.openSession();
        //定位sql(namespace值.id值),并执行会话
        //selectOne  执行查询的sql，并且只会返回一个结果
        Object o = sqlSession.selectOne("userMapper.getById",3);
        System.out.println(o);
        //selectlist  执行查询的sql,并且返回多个结果
        List<Object> obj = sqlSession.selectList("userMapper.getAll");
        System.out.println(obj);
        for (Object u: obj) {
            System.out.println(u);
        }

        Object obje = sqlSession.selectOne("userMapper.getByName","xiongda");
        System.out.println(obje);

        sqlSession.close();
        in.close();
    }
}
